<template>
  <div class="container">
    <div class="title">
      <span>修改轮播图</span> <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
    </div>
    <el-divider></el-divider>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" status-icon ref="form" label-width="100px" v-loading="loading" @submit.native.prevent>
            <el-form-item label="标题" prop="title">
              <el-input size="medium" v-model="form.title" placeholder="请输入轮播图标题"></el-input>
            </el-form-item>
			

			<el-form-item label="URL" prop="url">
              <el-input size="medium" v-model="form.url" placeholder="请输入轮播图URL" disabled="disabled"></el-input>
			  <el-upload 
			  ref="upload" 
			  class="upload-demo" 
			  :action="2222" 
			  :headers="headers" 
			  :on-success="onSuccessFile" 
			  :on-error="onErrorFile" 
			  :before-upload="beforeUpload" 
			  :on-progress="onProgressFile" 
			  name="file" 
			> 
			  <el-button 
				size="mini" 
				icon="el-icon-folder-add" 
				plain 
				type="primary" 
				>上传</el-button 
			  > 
			</el-upload>
			
			<img class="img" :src="form.url" style="width: 200px;"/>
			
            </el-form-item>
			
			<el-form-item label="序号" prop="st">
			   <el-input size="medium" v-model="form.st" placeholder="请输入轮播图序号"></el-input>
              </el-input>
            </el-form-item>
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  Shift
} from '@/model/shift'

export default {
  components: {},
  // 定义变量
  props: {
    // 子组件定义的变量通过父组件赋值
    editShiftID: {
      type: Number,
    },
  },
  data() {
    return {
      loading: false,
      form: {},
    }
  },
  async mounted() {
    this.loading = true
    const res = await Shift.getById(this.editShiftID)
    this.form = res
    this.loading = false
  },
  methods: {
  	 async beforeUpload(file){
		let fd = new FormData();
		fd.append('files',file);//传⽂件
		fd.append('no','11111');//传其他参数
		
    	const logos = await Shift.upload(fd)
	    //this.img=logos[0].url
		 console.info(2222);
		 console.info(logos);
		  console.info(logos[0].url);
		 //this.img=logos[0].url
		  this.form.url=logos[0].url
		
	
		//axios.post('cloudFile/recruit/file',fd).then(function(res){
		//	this.$data.img=res[0].url
		//	alert('成功');
		//	alert(this.$data.img);
		//})
		return false //屏蔽了action的默认上传
	},
	
    async submitForm() {
      const res = await Shift.update(this.editShiftID, this.form)
      if (res.code < window.MAX_SUCCESS_CODE) {
        this.$message.success(`${res.message}`)
        // 返回轮播图列表页面
        this.$emit('editClose')
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      // 由于showEdit这个变量是在父组件定义的，因此不能在子组件改变该变量的值，可以使用事件传递的方式，当点击了返回按钮，触发的事件处理机制交给父组件去处理
      this.$emit('editClose')
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
